package com.iot.service.impl;

import com.iot.domain.TeacherUser;
import com.iot.domain.TeacherUserVo;
import com.iot.mapper.UserMapper;
import com.iot.service.UserService;
import com.iot.util.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;


    /*登录教师的用户*/
    @Override
    public TeacherUser login(TeacherUserVo teacherUser) throws Exception {



        TeacherUser user1 = userMapper.login(teacherUser);
        System.out.println("用户"+user1.getPassword());
        //通过md5算法进行判断  密码是否相同
        if (user1 != null && Md5.verify(teacherUser.getPassword(), "iot", user1.getPassword())) {
            return user1;
        }
        return null;

    }

    /*注册用户*/
    @Override
    public Boolean register(TeacherUserVo teacherUserVo) {


        //在注册之前要对数据库中用户进行查询  判断是否存在 该用户
        //根据 手机号判断是否存在该用户

        TeacherUser userByPhone = userMapper.findUserByPhone(teacherUserVo.getPhone());
        if (userByPhone==null){
        //将用户的存储到数据库中
            Date date = new Date();
            //进行md5加密
            try {
                teacherUserVo.setPassword(Md5.md5(teacherUserVo.getPassword(),"iot"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            teacherUserVo.setCreateTime(date);
            teacherUserVo.setUpdateTime(date);
        userMapper.register(teacherUserVo);

        return true;

        }else {

            return false ;
        }

    }
    /*查询是否存用户*/
    @Override
    public TeacherUser  findUserByPhone(String phone) {

    return     userMapper.findUserByPhone(phone);
    }

    @Override
    public void cancelUser(String phone) {

         userMapper.cancelUser(phone);
    }


}
